Kinetis SDK API Reference Manual  1.0.0-beta
Freescale Semiconductor, Inc.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
PIT Peripheral Driver

The section describes the programming interface of the PIT Peripheral driver. More...

Data Structures

struct  pit_user_config_t
 PIT timer configuration structure. More...
 

Typedefs

typedef void(* pit_isr_callback_t )(void)
 PIT ISR callback function typedef.
 

Initialize and Shutdown

void pit_init_module (bool isRunInDebug)
 Initialize PIT module. More...
 
void pit_init_channel (uint32_t timer, const pit_user_config_t *config)
 Initialize PIT channel. More...
 
void pit_shutdown (void)
 Disable PIT module and gate control. More...
 

Timer Start and Stop

void pit_timer_start (uint32_t timer)
 Start timer counting. More...
 
void pit_timer_stop (uint32_t timer)
 Stop timer counting. More...
 

Timer Period

void pit_set_timer_period_us (uint32_t timer, uint32_t us)
 Set timer period in microsecond units. More...
 
uint32_t pit_read_timer_us (uint32_t timer)
 Read current timer value in microsecond units. More...
 

ISR Callback Function

void pit_register_isr_callback_function (uint32_t timer, pit_isr_callback_t function)
 Register pit isr callback function. More...
 

PIT Peripheral Driver

Overview

The pit driver is used to configure pit timers. It provides an easy way to make necessary module initializations and configure timer periods.

Initialization

To initialize the pit module, call pit_init_module first. This function will enable the PIT module and clock automatically. The parameter passed in pit_init_module will configure timers run or stop in debug mode. To use one timer channel, pit_init_channel should be called to init that channel.
This is example code for initializing and configuring the driver:
// Define device configuration.
const pit_config_t pitInit = {
isInterruptEnabled = false,// Disable timer interrupt.
isTimerChained = false, // Meaningless for timer 0.
periodUs = 20U // Set timer period to 20 us.
};
// Initialize PIT module. Timers will stop running in debug mode.
// Initialize PIT timer 0.
pit_init_channel(0, &pitInit);

Timer Period

The pit driver provides four ways to set the timer period.

  1. The pit_init_channel function sets the timer period in units of microseconds. It is only applicable when initializing the channel.
  2. The void pit_set_timer_period_us(uint32_t timer, uint32_t us) function sets the timer period in microsecond units. It is applicable at any time.
  3. The void pit_set_lifetime_timer_period_us(uint64_t us) function sets the lifetime timer period in microsecond units. It only supports specific chips. Check the reference manual before using this function.
  4. The void pit_hal_set_timer_period_count(uint32_t timer, uint32_t count) function sets the timer period in units of count. To use this function, the fsl_pit_hal.h needs to be included.
To read the current timer counting value in microsecond units, call uint32_t pit_read_timer_us(uint32_t timer).

Timer Operation

After timer setting is finished, call void pit_timer_start(uint32_t timer) to start timer counting. Call void pit_timer_stop(uint32_t timer) to stop it at any time.
If you want to close the PIT module entirely, call void pit_shutdown(void). This will disable the PIT module and the clock gate.

Data Structure Documentation

struct pit_user_config_t

Define structure PitConfig and use pit_init_channel() to make necessary initializations. You may also use remaining functions for PIT configuration.

Note
the timer chain feature is not valid in all devices, please check fsl_pit_features.h for accurate setting. If it's not valid, the value set here will be bypassed inside function pit_init_channel().

Data Fields

bool isInterruptEnabled
 Timer interrupt 0-disable/1-enable.
 
bool isTimerChained
 Chained with previous timer, 0-not/1-chained.
 
uint32_t periodUs
 Timer period in unit of microseconds.
 

Function Documentation

void pit_init_module ( bool  isRunInDebug)

This function must be called before calling all the other PIT driver functions. This function un-gates the PIT clock and enables the PIT module. The isRunInDebug passed into function will affect all timer channels.

Parameters
isRunInDebugTimers run or stop in debug mode.
  • true: Timers continue to run in debug mode.
  • false: Timers stop in debug mode.
void pit_init_channel ( uint32_t  timer,
const pit_user_config_t config 
)

This function initialize PIT timers by channel. Pass in timer number and its config structure. Timers do not start counting by default after calling this function. Function pit_timer_start must be called to start timer counting. Call pit_set_timer_period_us to re-set the period.

Here is an example demonstrating how to define a PIT channel config structure:

pit_user_config_t pitTestInit = {
// Only takes effect when chain feature is available.
// Otherwise, pass in arbitrary value(true/false).
.isTimerChained = false,
// In unit of microseconds.
.periodUs = 1000,
};
Parameters
timerTimer channel number.
configPIT channel configuration structure.
void pit_shutdown ( void  )

This function disables all PIT interrupts and PIT clock. It then gates the PIT clock control. pit_init_module must be called if you want to use PIT again.

void pit_timer_start ( uint32_t  timer)

After calling this function, timers load period value, count down to 0 and then load the respective start value again. Each time a timer reaches 0, it generates a trigger pulse and sets the timeout interrupt flag.

Parameters
timerTimer channel number.
void pit_timer_stop ( uint32_t  timer)

This function stops every timer counting. Timers reload their periods respectively after the next time they call pit_timer_start.

Parameters
timerTimer channel number.
void pit_set_timer_period_us ( uint32_t  timer,
uint32_t  us 
)

The period range depends on the frequency of PIT source clock. If the required period is out of range, use the lifetime timer, if applicable.

Parameters
timerTimer channel number.
usTimer period in microseconds.
uint32_t pit_read_timer_us ( uint32_t  timer)

This function returns an absolute time stamp in microsecond units. One common use of this function is to measure the running time of a part of code. Call this function at both the beginning and end of code; the time difference between these two time stamps is the running time (Make sure the running time will not exceed the timer period). The time stamp returned is up-counting.

Parameters
timerTimer channel number.
Returns
Current timer value in microseconds.
void pit_register_isr_callback_function ( uint32_t  timer,
pit_isr_callback_t  function 
)

System default ISR interfaces are already defined in fsl_pit_irq.c. Users can either edit these ISRs or use this function to register a callback function. The default ISR runs the callback function if there is one installed.

Parameters
timerTimer channel number.
functionPointer to pit isr callback function.